<%-- 
    Document   : registration
    Created on : Jan 21, 2012, 12:02:30 AM
    Author     : eugene
--%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Registration</title>
        <script type="text/javascript" src="js/fieldsChecker.js"></script>
        <script type="text/javascript">
            var fieldChecker;
            
            $(document).ready(function() {
                fieldChecker = new fieldsChecker("login", "password", "email", "loginError", "passwordError", "emailError");
            }).bind("keypress", function(e) {
                e = e || window.event;
                if(e.which == 13 || e.keyCode == 13) {
                    register();
                }
            });
            
            function register() {
                if(fieldChecker.checkFields()){
                    $.ajax({
                        url: "add_user.html",
                        data: {name: $.trim(fieldChecker.$login.val()), 
                            password: $.trim(fieldChecker.$password.val()),
                            email: $.trim(fieldChecker.$email.val())},
                        success: function(resultMsg) {
                            if(resultMsg == "ok") {
                                login();
                            } else {
                                $("#errorDiv").html(resultMsg);
                            }
                        }
                    });
                }
            }
            
            function login() {
                $.ajax({
                    url: "j_spring_security_check",
                    data: {j_username: $.trim(fieldChecker.$login.val()),
                            j_password: $.trim(fieldChecker.$password.val())},
                    type: "post",
                    success: function() {
                        window.location = "welcome/" + $.trim(fieldChecker.$login.val()) + ".html";
                    }
                });
            }
        </script>
    </head>
    <body>
        <div class="form-horizontal login-div" >
            <h2>Registration</h2>
            <hr style="margin-top: 0px; margin-bottom: 20px;"/>
            <jsp:include page="user_add_form.jsp" />
            <div class="form-actions" style="margin-top: 10px;">
                <input class="btn btn-primary" type="button" onclick="register()" value="Register" />
            </div>
        </div>
    </body>
</html>